package edu.uic.kott.activities;

import edu.uic.keyoutcomestrackertool.R;
import edu.uic.kott.db.KottDataSource;
import edu.uic.kott.model.ClassActivity;
import edu.uic.kott.model.Course;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class ActivityDetails extends Activity {

	private static final String LOGTAG = "KOTT";

	KottDataSource datasource;

	private String id;
	private String className;
	private String activityType;
	private String activityName;
	private String dueDate;
	private String description;
	private String maxGrade;
	private String obtGrade;
	private String completed;
	private String option;

	private TextView classNameTextView;
	private TextView activityTypeTextView;
	private TextView activityNameTextView;
	private TextView dueDateTextView;
	private TextView descriptionTextView;
	private TextView maxGradeTextView;

	private EditText obtGradeEditText;

	private CheckBox completedCheckBox;

	private Button submit;

	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_details);

		classNameTextView = (TextView) findViewById(R.id.rActivityClassTextView);
		activityTypeTextView = (TextView) findViewById(R.id.rActivityTypeTextView);
		activityNameTextView = (TextView) findViewById(R.id.rActivityNameTextView);
		dueDateTextView = (TextView) findViewById(R.id.rActivityDueDateTextView);
		descriptionTextView = (TextView) findViewById(R.id.rActivityDescTextView);
		maxGradeTextView = (TextView) findViewById(R.id.rActivityMaxGradeTextView);
		obtGradeEditText = (EditText) findViewById(R.id.rActivityGradeEditText);
		completedCheckBox = (CheckBox) findViewById(R.id.rActivityCompleteCheckBox);
		submit = (Button) findViewById(R.id.submitGradeButton);

		datasource = new KottDataSource(this);
		Bundle extras = getIntent().getExtras();
		ClassActivity acvty = new ClassActivity();

		if (extras != null){
			id = extras.getString("activityId");
			Log.i(LOGTAG,"Activity id is:" + id);
			acvty = datasource.getActivityDetails(id);

			className = acvty.getCourse();
			activityType = acvty.getActivityType();
			activityName = acvty.getActivityName();			
			dueDate = acvty.getDueDate();
			description = acvty.getActivityDescription();
			completed = acvty.isCompleted();
			maxGrade = acvty.getMaxGrade();
			obtGrade = acvty.getObtGrade();

			Log.i(LOGTAG,maxGrade+obtGrade);

			classNameTextView.setText(className);
			activityTypeTextView.setText(activityType);
			activityNameTextView.setText(activityName);
			dueDateTextView.setText(dueDate);
			descriptionTextView.setText(description);
			maxGradeTextView.setText(maxGrade);
			obtGradeEditText.setText(obtGrade);

			if("true".equalsIgnoreCase(completed))
				completedCheckBox.setChecked(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_edit_delete, menu);		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if(item.getOrder() == 100){	
			Log.i(LOGTAG,"EDIT menu item clicked");
		}
		else if(item.getOrder() == 200)
		{
			Log.i(LOGTAG,"Calling DELETE course from Course Details class");
			AlertDialog.Builder builder = new AlertDialog.Builder(ActivityDetails.this);
			builder.setTitle(R.string.dialogTitle_delete);		//set message title
			builder.setMessage(R.string.dialogMsg_deleteActivity); //set message content

			builder.setCancelable(true);
			builder.setNegativeButton(R.string.cancel, null);

			builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					if(datasource.deleteActivity(id, "activity")){
						/*Intent intent = new Intent(ActivityDetails.this, CourseActivities.class);
						startActivity(intent);*/
						finish();
					}					
				}
			});

			AlertDialog confirmDialog = builder.create();
			confirmDialog.show();

		}
		return false;
	}

}
